﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SocketPostData.BLL
{
    public class HJ212_2017
    {
        public string header { get; set; }

        public string data_length { get; set; }

        public Dictionary<string, string> DATA_HEAD { get; set; }

        public Dictionary<string, string> CP { get; set; }

        public string CRC { get; set; }

        public static bool Crc16(string Text)
        {
            bool flag6;
            try
            {
                string str = Text.Substring(Text.Length - 4, 4);
                Text = Text.Substring(Text.IndexOf("QN"));
                Text = Text.Substring(0, Text.Length - 4);
                byte[] bytes = Encoding.ASCII.GetBytes(Text);
                int num = 0xffff;
                int index = 0;
                while (true)
                {
                    if (index >= bytes.Length)
                    {
                        string str2 = $"{num:X}";
                        int length = str2.Length;
                        while (true)
                        {
                            if (length >= 4)
                            {
                                if (str2 == str)
                                {
                                    flag6 = true;
                                }
                                else
                                {
                                    Console.WriteLine("校验码有误：" + str);
                                    Console.WriteLine("待校验数据：" + Text);
                                    Console.WriteLine("计算校验码：" + str2);
                                    flag6 = false;
                                }
                                break;
                            }
                            str2 = "0" + str2;
                            length++;
                        }
                        break;
                    }
                    num = (num >> 8) ^ bytes[index];
                    int num3 = 0;
                    while (true)
                    {
                        if (num3 >= 8)
                        {
                            index++;
                            break;
                        }
                        int num4 = num & 1;
                        num = num >> 1;
                        if (num4 == 1)
                        {
                            num ^= 0xa001;
                        }
                        num3++;
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("数据校验：数据有问题，" + Text);
                flag6 = false;
            }
            return flag6;
        }

        public bool DecodeData(string Text)
        {
            bool flag4;
            try
            {
                Text = Text.ToUpper().Replace(";CP=", "").Replace(" ", "");
                this.header = Text.Substring(0, 2);
                this.data_length = Text.Substring(2, 4);
                char[] separator = new char[] { '&', '&' };
                string[] strArray = Text.Substring(6, Text.Length - 6).Split(separator, StringSplitOptions.RemoveEmptyEntries);
                char[] chArray2 = new char[] { ';' };
                string[] strArray2 = strArray[0].Split(chArray2);
                this.DATA_HEAD = new Dictionary<string, string>();
                int index = 0;
                while (true)
                {
                    if (index >= strArray2.Length)
                    {
                        char[] chArray4 = new char[] { ';' };
                        string[] strArray3 = strArray[1].Split(chArray4);
                        this.CP = new Dictionary<string, string>();
                        int num2 = 0;
                        while (true)
                        {
                            if (num2 >= strArray3.Length)
                            {
                                this.CRC = strArray[2];
                                flag4 = true;
                                break;
                            }
                            char[] chArray5 = new char[] { ',' };
                            string[] strArray5 = strArray3[num2].Split(chArray5);
                            int num3 = 0;
                            while (true)
                            {
                                if (num3 >= strArray5.Length)
                                {
                                    num2++;
                                    break;
                                }
                                char[] chArray6 = new char[] { '=' };
                                string[] strArray6 = strArray5[num3].Split(chArray6);
                                this.CP.Add(strArray6[0], strArray6[1]);
                                num3++;
                            }
                        }
                        break;
                    }
                    char[] chArray3 = new char[] { '=' };
                    string[] strArray4 = strArray2[index].Split(chArray3);
                    this.DATA_HEAD.Add(strArray4[0], strArray4[1]);
                    index++;
                }
            }
            catch (Exception)
            {
                Console.WriteLine("解码失败：数据有问题，" + Text);
                flag4 = false;
            }
            return flag4;
        }

        public static string GetHj212Crc16(string text)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(text);
            int num = 0xffff;
            int index = 0;
            while (true)
            {
                if (index >= bytes.Length)
                {
                    string str = $"{num:X}";
                    for (int i = str.Length; i < 4; i++)
                    {
                        str = "0" + str;
                    }
                    return str;
                }
                num = (num >> 8) ^ bytes[index];
                int num3 = 0;
                while (true)
                {
                    if (num3 >= 8)
                    {
                        index++;
                        break;
                    }
                    int num4 = num & 1;
                    num = num >> 1;
                    if (num4 == 1)
                    {
                        num ^= 0xa001;
                    }
                    num3++;
                }
            }
        }

        public static string GetHj212DataLength(string text)
        {
            int length = text.Length;
            return ((length >= 0x3e8) ? length.ToString() : ("0" + length.ToString()));
        }

    }
}
